﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;

namespace Neudesic.Pulse
{
    /// <summary>
    /// Provides Url services for interacting with the Pulse social framework
    /// </summary>
    public static class UriUtility
    {

        /// <summary>
        /// Combines two uris.
        /// </summary>
        /// <param name="former">The former.</param>
        /// <param name="latter">The latter.</param>
        /// <returns></returns>
        public static string CombineUri(string former, string latter)
        {
            // Appending the latter string to the form string,
            // while making sure there is a single slash char seperating the latter and the former.
            // This method behaves differently than new Uri(baseUri, relativeUri)
            // as CombineUri simply appends, whereas new Uri() actually replaces the last segment
            // of the its base path with the relative uri.
            StringBuilder builder = new StringBuilder();
            if (former.Length > 0 && latter.Length > 0)
            {
                if (former[former.Length - 1] == '/' && latter[0] == '/')
                {
                    builder.Append(former, 0, former.Length - 1);
                    builder.Append(latter);
                    return builder.ToString();
                }

                if (former[former.Length - 1] != '/' && latter[0] != '/')
                {
                    builder.Append(former);
                    builder.Append('/');
                    builder.Append(latter);
                    return builder.ToString();
                }
            }

            return former + latter;
        }

        /// <summary>
        /// Gets the parent path.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns></returns>
        public static string GetParentPath(string uri)
        {
            if (string.IsNullOrEmpty(uri) || uri == "/")
                return string.Empty;

            int lastSlash = uri.LastIndexOf("/");

            if (lastSlash >= 0)
                return uri.Substring(0, lastSlash);

            return string.Empty;
        }

        /// <summary>
        /// Gets a uri with querystring paramaters set with specific values.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public static string SetQueryParameters(string uri, params KeyValuePair<string, string>[] parameters)
        {
            if (parameters.Length == 0)
                return uri;

            try
            {
                Uri resource = new Uri(uri);
                NameValueCollection parms = HttpUtility.ParseQueryString(resource.Query);
                foreach (KeyValuePair<string, string> item in parameters)
                {
                    parms[item.Key] = item.Value; // Assumes no repeat of values           
                }
                return resource.AbsoluteUri + "?" + parms.ToString();
            }
            catch (Exception)
            {
                return uri;
            }
        }

        /// <summary>
        /// Creates a query parameter object from a name and value pair.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static KeyValuePair<string, string> CreateQueryParameter(string name, string value)
        {
            return new KeyValuePair<string, string>(name, value.ToString());
        }

        /// <summary>
        /// Gets the sized avatar URI.
        /// </summary>
        /// <param name="avatarUri">The avatar URI.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public static string GetSizedAvatarUri(string avatarUri, int size)
        {
            return SetQueryParameters(avatarUri, size.ToQueryParameter("size"));
        }

    }
}
